/**
 * 定义历史数据审核模块
 */
(function(window, angular, undefined){
	var module = angular.module('water.apps.voc.reportHistory.reportDataAuditManager', ['water.apps.common.report.reportBase','water.entity.monitorData']);
	
	module.controller('reportDataAuditManagerController',function($rootScope,$scope,$controller,$dateUtil,$wasteGasMonitorDataService,$state,$stateParams){

		var control = $controller("commonReportAuditController",{$scope:$scope});
		
		$scope.dataAPIName = "hourListGetter";
		$scope.dataAuditAPIName = "hourAuditListGetter";
		$scope.dataExportAPIName = "exportHourExcel";
		/**
		 * 数据项
		 */
		$scope.dataType=[];
		/**
		 * 数据主键集合
		 */
		$scope.keys = [];
		/**
		 * 数据标识
		 */
		$scope.dataFlag;
		/**
		 * 数据状态
		 */
		$scope.dataState="0";
		/**
		 * 开始日期
		 */
		$scope.startDate;
		/**
		 * 结束日期
		 */
		$scope.endDate;
		/**
		 * 开始小时
		 */
		$scope.startHour;
		/**
		 * 结束小时
		 */
		$scope.endHour;
		
		/**
		 * 数据来源变更事件 
		 */
		$scope.onDataStateChange = function(){
			var queryObject = $scope.getQueryObject();
			if($scope.dataState==1)  //已审核，审核后数据
			{
				$scope.getAuditReport(queryObject);
			}else{ 	 	 //未审核、原始数据
				$scope.getReport(queryObject);
			}
		}
		
		/**
		 * 查询按钮点击
		 */
		$scope.onSearchClick = function(){
			//获取点位该时间段内的数据是否已经审核过
			var queryObject = $scope.getQueryObject();
			$wasteGasMonitorDataService.queryDataState(queryObject,function(data){
				$scope.dataState=data.data;
				$scope.onDataStateChange();
			});
		}
		

		/**
		 * 审核按钮事件，用于切换页面
		 */
		$scope.onDataAudit = function(){
			var data = {};
			data.startDate = $scope.startDate;
			data.endDate = $scope.endDate;
			data.startHour = $scope.startHour;
			data.endHour = $scope.endHour;
			data.dataState = $scope.dataState;
			$state.go("Report.history.dataAuditDetail",{curRow:data});
		}
		
		/**
		 * 监听左侧列表选择
		 */
		$scope.oNdataPramChanged = $scope.$on('dataPramChanged', function(event, station) {
			$scope.onStationSelectedBeforeQuery(station);
			pollFreshLag=true;
	    });
		
		/**
		 * 站点改变后，更新构建报表的参数，比如表头，数据项等
		 */
		$scope.onStationSelectedBeforeQuery = function(station){
			$scope.selectedModel = station;
			$scope.parmList = station.parmToCheckList;
			var parmListNew=[];
			$scope.dataType=[];
			for(var i=0;i<$scope.parmList.length;i++){
				var parm=$scope.parmList[i];
				if(parm.name=="01"||parm.name=="02"||parm.name=="03"||parm.name=="04"||parm.name=="06"||parm.name=="08") {
					parm.colspan=2;
					$scope.dataType.push("实测值");
					$scope.dataType.push("折算值");
				}
				else{
					parm.colspan=1;
					$scope.dataType.push("实测值");
				}
				parmListNew.push(parm);
			}
			$scope.parmList= parmListNew;
			
			$scope.keys = [];
			for(var i=0;i<$scope.parmList.length;i++){
				var obj = $scope.parmList[i];
				if(obj.name=="01"||obj.name=="02"||obj.name=="03"||obj.name=="04"||obj.name=="06"||obj.name=="08") {
					$scope.keys.push("AVG_"+obj.name);
					$scope.keys.push("ZS_AVG_"+obj.name);
				}
				else{
					$scope.keys.push("AVG_"+obj.name);
				}
			}
			//初始化
			_init();
		};
		/**
		 * 获取查询参数
		 * forExcel 是否用于导出
		 */
		$scope.getQueryObject = function(forExcel){
			var parmsStr = $scope.selectedModel.parmToChecksStr;
			var queryObject = {
					"MN":$scope.selectedModel.MN,
					"PARMS":parmsStr,
					"START":$scope.startDate + " " + $scope.startHour + ":00:00",
					"END":$scope.endDate + " " + ($scope.endHour+1) + ":00:00",
				};
			return queryObject;
		}
		
		/**
		 * 初始化
		 */
		function _init(){
			if($stateParams.curRow!=null)
			{
				$scope.startDate = $stateParams.curRow.startDate;
				$scope.endDate = $stateParams.curRow.endDate;
				$scope.startHour = $stateParams.curRow.startHour;
				$scope.endHour = $stateParams.curRow.endHour;
			}else{
				var endDate = $dateUtil.addDay(new Date(),-1);
				var startDate = endDate;
				$scope.startDate = $dateUtil.formatDate(startDate,'yyyy-MM-dd');
				$scope.endDate = $dateUtil.formatDate(endDate,'yyyy-MM-dd');
				$scope.startHour = 0;
				$scope.endHour = 23;
			}
			//获取点位该时间段内的数据是否已经审核过
			var queryObject = $scope.getQueryObject();
			$wasteGasMonitorDataService.queryDataState(queryObject,function(data){
				$scope.dataState=data.data;
    			$scope.onDataStateChange();
			});
			
		}


	});
}(window, angular));	